package com.yanghui.dtracing.plugin.thread;

import com.yanghui.dtracing.core.EnhancedInstance;
import com.yanghui.dtracing.core.opentracing.*;
import com.yanghui.dtracing.core.plugin.interceptor.IInstanceMethodAroundInterceptor;
import com.yanghui.dtracing.core.plugin.interceptor.MethodInterceptResult;
import com.yanghui.dtracing.core.utils.AsynAcross;

import java.lang.reflect.Method;

public class ThreadInstanceMethodAroundInterceptor implements IInstanceMethodAroundInterceptor {
    @Override
    public void beforeMethod(Object thisObj, Method method, Object[] allArguments, MethodInterceptResult result) throws Throwable {
        if (!(thisObj instanceof EnhancedInstance)) {
            return;
        }
        EnhancedInstance enhancedInstance = (EnhancedInstance)thisObj;
        AsynAcross asynAcross = (AsynAcross)enhancedInstance.getDynamicField();

        if(asynAcross.getSpan() != null){
            Tracing.getTracer().activateSpan(asynAcross.getSpan());
        }
        if(asynAcross.getAtomicInteger() != null){
            ScopeContext.putIncrement(asynAcross.getAtomicInteger());
        }
    }
}
